#include "command.h"
#define CMDNUM 9
#include "client_transfile.h"

int getFileLength(char* filePath){
    int fd=open(filePath,O_RDWR|O_CREAT,0666);
    ERROR_CHECK(fd,-1,"open");
    struct stat statbuf;
    fstat(fd,&statbuf);
    int length=statbuf.st_size;
    close(fd);
    return length;
}

int sendCmd(int netfd,ENTRY* entry){
    int ret = send(netfd,entry,sizeof(ENTRY),0);
    return ret;
}

int recvCmd(int netfd){
    train_t train;
    bzero(&train,sizeof(train));
    recv(netfd,&train.dataLength,sizeof(int),0);
    recv(netfd,&train.data,train.dataLength,0);
    printf("%s\n",train.data);
    return 0;
}

int whichCmd(char* sentence,ENTRY* entry){
    char* cmd=strtok(sentence," ");
    char* obj=strtok(NULL," ");
    if(obj==NULL){
        int length=strlen(cmd);
        cmd[length-1]='\0';
    }
    char* cmdList[CMDNUM]={"cd","ls","puts","gets","remove","pwd","mkdir","rmdir","exit"};
    int seq=-1;
    for(int i=0;i<CMDNUM;++i){
        if(strcmp(cmd,cmdList[i])==0){
            seq=i;
            break;
        }
    }
    ERROR_CHECK(seq,-1,"错误的命令！");
    
    switch(seq){
    case CD:
        entry->CMD=CD;
        entry->fileLength=-1;
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        break;
    case LS:
        entry->CMD=LS;
        entry->fileLength=-1;
        break;
    case PUTS:
        entry->CMD=PUTS;
        //todo获取文件长度
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        entry->fileLength=getFileLength(entry->data);
        Compute_file_md5(entry->data,entry->md5);
        break;
    case GETS:
        entry->CMD=GETS;
        //todo:判断文件长度，本地没有则为0，本地存在获取其长度
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        entry->fileLength=getFileLength(entry->data);
        Compute_file_md5(entry->data,entry->md5);
        break;
    case REMOVE:
        entry->CMD=REMOVE;
        entry->fileLength=-1;
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        break;
    case PWD:
        entry->CMD=PWD;
        entry->fileLength=-1;
        break;
    case MKDIR:
        entry->CMD=MKDIR;
        entry->fileLength=-1;
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        break;
    case RMDIR:
        entry->CMD=RMDIR;
        entry->fileLength=-1;
        ERROR_CHECK(obj,NULL,"缺少参数！");
        entry->dataLength=strlen(obj)-1;
        obj[entry->dataLength]='\0';
        strcpy(entry->data,obj);
        break;
    case EXIT:
        entry->CMD=EXIT;
        entry->fileLength=-1;
        break;
    }
    return 0;
}

int doCmd(int netfd,ENTRY* entry){
    switch(entry->CMD){
    case CD:
        recvCmd(netfd);
        break;
    case LS:
        lsClient(netfd);
        break;
    case PUTS:
        sendFile(netfd,entry->data);
        break;
    case GETS:
        recvFile(netfd,entry->data);
        break;
    case REMOVE:
        //
        break;
    case PWD:
        recvCmd(netfd);
        break;
    case MKDIR:
       // recvCmd(netfd);
        break;
    case RMDIR:
        //recvCmd(netfd);
        break;
    default:
        break;
    }
}

